#include "Ant.h"
#include "Food.h"
#include "Anthill.h"
#include "Area.h"
#include "Item.h"
#include <iostream>
#include <time.h>
#include <cstdlib> 

CAnt::CAnt(void)
{
	SetPosition(NULL);
	straight_way = 0;
	age = 0;
	food = 0;
}


CAnt::~CAnt(void)
{
}

void CAnt::act(int id){

	if (age > MAX_ANT_AGE){
		Die();
		return;
	}
	if ( id != m_iActID){
		std::cout << "Ant acting" << std::endl;
		CalcMove();
		CheckForFood();
		age++;
	}
	m_iActID = id;

}

void CAnt::CalcMove(){
	int Dir =0 ;
	int lastDir = 0;

	

	//Additional moves
	if( lwaypoints.size() > 0){
		lastDir=lwaypoints.back();
		if(food == 0){
		
			
			std::cout << "Searching food "<< std::endl;
			if(straight_way < MIN_STRAIGHT_WAY){
				Dir = lastDir;
			}
			else{
				straight_way = 0;
				for(int i = 0 ; i <= 10; i++){
					Dir = GetDir();
					if (Dir != lastDir){
						
						break;
				}
				
				}
				lwaypoints.push_back(Dir);
		}
	}
		else
		{
			std::cout << "Bring back food "<< std::endl;
			lwaypoints.pop_back();
			if (lastDir == 0) Dir = 2;
			else if (lastDir == 1) Dir = 3;
			else if (lastDir == 2) Dir = 0;
			else if (lastDir == 3) Dir = 1;

		}


	}

	//First Move
	else{
		//std::cout << " First Move "<< std::endl;
		Dir = GetDir();
		lwaypoints.push_back(Dir);
	}
	
	//std::cout << "Ant Moving from " << lastDir <<"to: " << Dir << std::endl;

	straight_way++;




	if (GetPosition() != NULL){
		if (Dir==0 && GetPosition()->GetNorthElement() != NULL){
			std::cout << "Ant Moving from to north" << std::endl;
			Move2(this->GetPosition()->GetNorthElement());
		
		}
		else if (Dir==1 && GetPosition()->GetEastElement() != NULL){
			Move2(this->GetPosition()->GetEastElement());
			std::cout << "Ant Moving from to east" << std::endl;
		
		}
		else if (Dir==2 && GetPosition()->GetSouthElement() != NULL){
			Move2(this->GetPosition()->GetSouthElement());
			std::cout << "Ant Moving from to south" << std::endl;
		
		}
		else if (Dir==3 && GetPosition()->GetWestElement() != NULL){
			Move2(this->GetPosition()->GetWestElement());
			std::cout << "Ant Moving from to west" << std::endl;
		
		}
	}

	if (lwaypoints.size() >0 && food == 0 && CheckForAnthill()){
		lwaypoints.clear();
	}

	if (food > 0 && CheckForAnthill()){
		lwaypoints.clear();
		food = 0;
	}


	
}

void CAnt::Move2(CArea * TargetArea){
	
	
	GetPosition()->DeleteItem(this);
	SetPosition(TargetArea);
	GetPosition()->InsertItem(this);


}

int CAnt::CheckForFood(){

	CFood* ptmpFood;

	ptmpFood = dynamic_cast<CFood*>(GetPosition()->HasFood());

	if(ptmpFood != NULL){
		food+= ptmpFood->GetFood();
		std::cout << "Found Food" << std::endl;
		return 1;
	
	}
	else{
		return 0;
	}


}

int CAnt::CheckForAnthill(){

	CAnthill* ptmpAnthill;

	ptmpAnthill = dynamic_cast<CAnthill*>(GetPosition()->HasAnthill());

	if(ptmpAnthill != NULL){
		
		std::cout << "Found Anthill" << std::endl;
		return 1;
	
	}
	else 
	{
		return 0;
	}

}


// Random number function
int CAnt::GetDir(){
	int zahl;
	zahl = std::rand() % 4;
	return zahl;

}

void CAnt::Die(){
	GetPosition()->DeleteItem(this);
	delete this;
}